templatePath = 'template-path/'
# fix CVAS-1394 ,在ie9下，console 未定义
window.console = window.console or do ->
    c = {}
    c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile = c.clear = c.exception = c.trace =
      c.assert = ->
    c
angular.module('UCX.Cmis', [
    'LocalStorageModule',
    'ui.bootstrap',
    'pascalprecht.translate'
    'toastr'
    'ng-context-menu'
    'file-model'
    'ngclipboard'
    'infinite-scroll'
  ]
)
.config ($httpProvider)->
  # 请求截断  暂时仅支持 angularjs 方法 $http的
  $httpProvider.interceptors.push('myInterceptor')
  true

.config ($translateProvider)->
  for language, languageData of CmisLanguage
    $translateProvider.translations language, languageData
#    useSanitizeValueStrategy different effects see here http://codepen.io/prophet_w/pen/oYKLed?editors=1010
#    $translateProvider.useSanitizeValueStrategy('escape') // will cause some problem see http://codepen.io/prophet_w/pen/oYKLed?editors=1010

#   if not set browser will warning the safety
    $translateProvider.useSanitizeValueStrategy('escapeParameters')
    
  $translateProvider.fallbackLanguage('US')
.config (toastrConfig)->
  angular.extend toastrConfig, {
#    https://github.com/Foxandxss/angular-toastr    Toastr customization
    positionClass: 'toast-top-center'
    closeButton : true
    escapeHtml : true
    allowHtml: true,
    maxOpened: 5
    preventDuplicates: false
    preventOpenDuplicates: false
#    autoDismiss: false
#    tapToDismiss: false
#    timeOut:0
#    extendedTimeOut:0
  }
.config (localStorageServiceProvider)->
  dataStorage = ''
  try
    if window.localStorage
      if window.localStorage? && typeof window.localStorage != 'undefined'
        dataStorage = 'localStorage'
      else
        dataStorage = 'sessionStorage'
      localStorageServiceProvider.setStorageType(dataStorage)
  catch e
    console.log e

.directive 'cmis', (CmisService, localStorageService, $translate)->
  {
    replace: true
    templateUrl: templatePath + "index.html"
    compile: (tElement, attrs, transclude, $scope)->
      #CBASE-264 日语 用Meiryo UI字体 又改成 Meiryo了 ..
#      console.log('attrs',attrs)
      if attrs.lang=='JP'
        $('#cmis-container').css('font-family','Meiryo')
      if localStorageService.isSupported
        if attrs.lang
          if !CmisLanguage[attrs.lang]
            localStorageService.set "language", 'US'
          else
            localStorageService.set "language", attrs.lang
        else
          localStorageService.set "language", 'US'

        # add debug mode
        cloudReadingDebugMode = localStorageService.get "CloudReadingDebug"
        if !cloudReadingDebugMode
          localStorageService.set("CloudReadingDebug",false)
          console.log = ()->
            return false

      if attrs.url.indexOf("{{") != -1
        return
      else
        CmisService.setConfig(
          attrs.url,
          attrs.token,
          attrs.path,
          attrs.baseUrl,
          attrs.isDrmRoom
          attrs.messageToMember
          attrs.showMembers
          attrs.roomLink
          attrs.previewDid
          attrs.allowExtension
          attrs.repository
          attrs.membershipType
          attrs.allowDownload
          attrs.filePickerModal
          attrs.device
          attrs.pluginVersion
          attrs.fromFillAndSign
          attrs.cwebToolsApi
          attrs.pluginUi
          attrs.fileType
          attrs.fileSelectMode
        )
    controller: 'directiveController'
  }
.directive 'showFocus', ($timeout)->
  (scope, element, attrs)->
    scope.$watch attrs.showFocus, (newValue)->
      $timeout ()->
        newValue && $(element).focus()
      ,
        true